using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.IO;

namespace kb_24_the_game
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        #region Variabele Declaratie

        private GraphicsDeviceManager graphics;
        private SpriteBatch spriteBatch;
        private int optellen = 0;
        private MainMenu mainMenu;
        private TextLader textLader;
        private KeyboardState keyState;
        private bool volgendeTekst;
        private string eindeTekstTeken = "##";
        public bool klaarvoorlevelVolgendLevel = false;
        private bool eersteKeerLaden = true;
        private bool klaarDusReset = true; 
        private string tussenSchermTekst = "";
        private Texture2D achtergrondLaadScherm, achtergrondScherm2, achtergrondScherm3,achtergrondScherm4, achtergrondScherm5;
        public Gamestate CurrentgameState;
        private SpriteFont FlashBacks;
        private Vector2 FlashBackTextPosition;
        private Level1 level1;
        private Level2 level2;
        private Level3 level3;
        private Level4 level4;
        private EndCredits endCredits;
        private MusicPlayer genericMusicPlayer;
        

        public enum Gamestate
        {
            MainMenu,
            Level1,
            Level2,
            Level3,
            Level4,
            Flashback1,
            Flashback2,
            Flashback3,
            Flashback4,
            Flashback5,
            EndCredits,
        }

        #endregion Variabele Declaratie


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.IsFullScreen = false;
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
            graphics.ApplyChanges();
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            textLader = new TextLader();
            mainMenu = new MainMenu();
            level1 = new Level1(this.Content);
            level2 = new Level2(this.Content);
            level3 = new Level3(this.Content);
            level4 = new Level4(this.Content);
            endCredits = new EndCredits(this.Content);
            CurrentgameState = Gamestate.MainMenu;
            eersteKeerLaden = true;
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
            /// </summary>
            protected override void LoadContent()
            {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            mainMenu.graphics = this.GraphicsDevice;
            mainMenu.spriteBatch = this.spriteBatch;
            mainMenu.content = this.Content;
            mainMenu.Load();
            FlashBacks = Content.Load<SpriteFont>("AlgemeneTextures/tussenSchermTekst");
            achtergrondLaadScherm = Content.Load<Texture2D>("Achtergronden/IntroClip02");
            achtergrondScherm2 = Content.Load<Texture2D>("Achtergronden/Flashback01");
            achtergrondScherm3 = Content.Load<Texture2D>("Achtergronden/Flashback02");
            achtergrondScherm4 = Content.Load<Texture2D>("Achtergronden/Flashback03");
            achtergrondScherm5 = Content.Load<Texture2D>("Achtergronden/EndFlashback");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            MouseState mouse = Mouse.GetState();

            #region Gamestate switch

            switch (CurrentgameState)
            {
                case Gamestate.MainMenu:
                    IsMouseVisible = true;
                    klaarDusReset = true;
                    volgendeTekst = true;
                    if (eersteKeerLaden == true)
                    {
                        genericMusicPlayer = new MusicPlayer(Content, 0);
                        genericMusicPlayer.PlayMusic();
                    }
                    eersteKeerLaden = false;
                    mainMenu.Update(mouse);
                    if (mainMenu.isExitClicked == true)
                    {
                        this.Exit();
                    }
                    if (mainMenu.isStartClicked == true)
                    {
                        CurrentgameState = Gamestate.Flashback1;
                        eersteKeerLaden = true;
                    }
                    break;

                #region flashbacks

                case Gamestate.Flashback1:
                    if (eersteKeerLaden == true || volgendeTekst == true)
                    {
                        if (eersteKeerLaden)
                        {
                            genericMusicPlayer = new MusicPlayer(Content, 1);
                            genericMusicPlayer.PlayMusic();
                        }
                        klaarvoorlevelVolgendLevel = false;
                        eersteKeerLaden = false;
                        volgendeTekst = false;
                        string temp;
                        temp = textLader.VolgendeRegel("level1.txt");
                        if (!temp.Equals(eindeTekstTeken))
                        {
                            tussenSchermTekst = temp;
                            FlashBackTextPosition = textLader.Position();
                        }
                        else
                        {
                            CurrentgameState = Gamestate.Level1;
                        }
                    }
                    keyState = Keyboard.GetState();
                    CheckSpatie(this.keyState);
                    break;

                case Gamestate.Flashback2:
                    if (eersteKeerLaden == true || volgendeTekst == true)
                    {
                        if (eersteKeerLaden)
                        {
                            genericMusicPlayer = new MusicPlayer(Content, 2);
                            genericMusicPlayer.PlayMusic();
                        }
                        klaarvoorlevelVolgendLevel = false;
                        eersteKeerLaden = false;
                        volgendeTekst = false;
                        string temp;
                        temp = textLader.VolgendeRegel("level2.txt");
                        if (!temp.Equals(eindeTekstTeken))
                        {
                            tussenSchermTekst = temp;
                            FlashBackTextPosition = textLader.Position();
                        }
                        else
                        {
                            CurrentgameState = Gamestate.Level2;
                        }
                    }
                    keyState = Keyboard.GetState();
                    CheckSpatie(this.keyState);
                    break;
                case Gamestate.Flashback3:
                    if (eersteKeerLaden == true || volgendeTekst == true)
                    {
                        if (eersteKeerLaden)
                        {
                            genericMusicPlayer = new MusicPlayer(Content, 3);
                            genericMusicPlayer.PlayMusic();
                        }
                        klaarvoorlevelVolgendLevel = false;
                        eersteKeerLaden = false;
                        volgendeTekst = false;
                        string temp;
                        temp = textLader.VolgendeRegel("level3.txt");
                        if (!temp.Equals(eindeTekstTeken))
                        {
                            tussenSchermTekst = temp;
                            FlashBackTextPosition = textLader.Position();
                        }
                        else
                        {
                            CurrentgameState = Gamestate.Level3;
                        }

                    }                    
                    keyState = Keyboard.GetState();
                    CheckSpatie(this.keyState);
                    break;
                case Gamestate.Flashback4:
                    if (eersteKeerLaden == true || volgendeTekst == true)
                    {
                        if (eersteKeerLaden)
                        {
                            genericMusicPlayer = new MusicPlayer(Content, 4);
                            genericMusicPlayer.PlayMusic();
                        }
                        klaarvoorlevelVolgendLevel = false;
                        eersteKeerLaden = false;
                        volgendeTekst = false;
                        string temp;
                        temp = textLader.VolgendeRegel("level4.txt");
                        if (!temp.Equals(eindeTekstTeken))
                        {
                            tussenSchermTekst = temp;
                            FlashBackTextPosition = textLader.Position();
                        }
                        else
                        {
                            CurrentgameState = Gamestate.Level4;
                        }

                    }
                    keyState = Keyboard.GetState();
                    CheckSpatie(this.keyState);
                    break;
                case Gamestate.Flashback5:
                    if (eersteKeerLaden == true || volgendeTekst == true)
                    {
                        klaarvoorlevelVolgendLevel = false;
                        eersteKeerLaden = false;
                        volgendeTekst = false;
                        string temp;
                        temp = textLader.VolgendeRegel("level5.txt");
                        if (!temp.Equals(eindeTekstTeken))
                        {
                            tussenSchermTekst = temp;
                            FlashBackTextPosition = textLader.Position();
                        }
                        else
                        {
                            CurrentgameState = Gamestate.EndCredits;
                        }

                    }
                    keyState = Keyboard.GetState();
                        CheckSpatie(this.keyState);
                        break;

                #endregion flashbacks

                #region levels

                case Gamestate.Level1:
                    eersteKeerLaden = true;
                    volgendeTekst = true;
                    IsMouseVisible = false;
                    
                    if (level1.reset == true)
                    {
                        level1 = null;
                        level1 = new Level1(Content);
                    }
                    klaarvoorlevelVolgendLevel = level1.Update(gameTime);
                    if (klaarvoorlevelVolgendLevel == true)
                    {
                        CurrentgameState = Gamestate.Flashback2;
                    }

                    break;

                case Gamestate.Level2:
                    eersteKeerLaden = true;
                    volgendeTekst = true;
                    IsMouseVisible = false;
                    klaarvoorlevelVolgendLevel = level2.Update(gameTime);
                    if (klaarvoorlevelVolgendLevel == true)
                    {
                        CurrentgameState = Gamestate.Flashback3;
                    }
                    break;
                case Gamestate.Level3:
                    eersteKeerLaden = true;
                    volgendeTekst = true;
                    IsMouseVisible = false;
                    klaarvoorlevelVolgendLevel = level3.Update(gameTime);
                    if (klaarvoorlevelVolgendLevel == true)
                    {
                        CurrentgameState = Gamestate.Flashback4;
                    }
                    break;
                case Gamestate.Level4:
                    eersteKeerLaden = true;
                    volgendeTekst = true;
                    IsMouseVisible = false;
                    klaarvoorlevelVolgendLevel = level4.Update(gameTime);
                    if (klaarvoorlevelVolgendLevel == true)
                    {
                        CurrentgameState = Gamestate.Flashback5;
                    }
                    if (level4.reset == true)
                    {
                        level4 = null;
                        level4 = new Level4(Content);
                    }
                    break;
                #endregion levels

                case Gamestate.EndCredits:
                    IsMouseVisible = false;
                    eersteKeerLaden = true;
                    if (klaarDusReset == true)
                    {
                        klaarDusReset = ResetLevels();
                    }
                    klaarvoorlevelVolgendLevel = endCredits.Update(gameTime);
                    if (klaarvoorlevelVolgendLevel == true)
                    {
                        CurrentgameState = Gamestate.MainMenu;
                    }

                    break;
            }

                

            #endregion Gamestate switch

            base.Update(gameTime);
        }

        private bool ResetLevels()
        {
            level1 = null;
            level1 = new Level1(Content);
            level2 = null;
            level2 = new Level2(Content);
            level3 = null;
            level3 = new Level3(Content);
            level4 = null;
            level4 = new Level4(Content);
            return false;
        }

        private void CheckSpatie(KeyboardState keyboardState)
        {
            optellen++;
            if (optellen >= 10 && keyboardState.IsKeyDown(Keys.Space))
            {
                volgendeTekst = true;
                optellen = 0;
            }
            if (optellen > 100)
            {
                optellen = 11;
            }
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        ///

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();
            switch (CurrentgameState)
            {
                case Gamestate.MainMenu:
                    mainMenu.Draw();
                    break;

                case Gamestate.Level1:
                    level1.Draw(spriteBatch);
                    break;

                case Gamestate.Level2:
                    level2.Draw(spriteBatch);
                    break;
                case Gamestate.Level3:
                    level3.Draw(spriteBatch);
                    break;
                case Gamestate.Level4:
                    level4.Draw(spriteBatch);
                    break;

                case Gamestate.Flashback1:
                    spriteBatch.Draw(achtergrondLaadScherm, new Vector2(0, 0), Color.White);
                    spriteBatch.DrawString(FlashBacks, tussenSchermTekst, new Vector2(FlashBackTextPosition.X + 2, FlashBackTextPosition.Y + 2), Color.Yellow);
                    spriteBatch.DrawString(FlashBacks, tussenSchermTekst, FlashBackTextPosition, Color.Black);
                    break;

                case Gamestate.Flashback2:
                    spriteBatch.Draw(achtergrondScherm2, new Vector2(0, 0), Color.White);
                    spriteBatch.DrawString(FlashBacks, tussenSchermTekst, new Vector2(FlashBackTextPosition.X + 2, FlashBackTextPosition.Y + 2), Color.Yellow);
                    spriteBatch.DrawString(FlashBacks, tussenSchermTekst, FlashBackTextPosition, Color.Black);
                    break;
                case Gamestate.Flashback3:
                    spriteBatch.Draw(achtergrondScherm3, new Vector2(0, 0), Color.White);
                    spriteBatch.DrawString(FlashBacks, tussenSchermTekst, new Vector2(FlashBackTextPosition.X + 2, FlashBackTextPosition.Y + 2), Color.Yellow);
                    spriteBatch.DrawString(FlashBacks, tussenSchermTekst, FlashBackTextPosition, Color.Black);
                    break;
                case Gamestate.Flashback4:
                    spriteBatch.Draw(achtergrondScherm4, new Vector2(0, 0), Color.White);
                    spriteBatch.DrawString(FlashBacks, tussenSchermTekst, new Vector2(FlashBackTextPosition.X + 2, FlashBackTextPosition.Y + 2), Color.Yellow);
                    spriteBatch.DrawString(FlashBacks, tussenSchermTekst, FlashBackTextPosition, Color.Black);
                    break;
                case Gamestate.Flashback5:
                    spriteBatch.Draw(achtergrondScherm5, new Vector2(0, 0), Color.White);
                    spriteBatch.DrawString(FlashBacks, tussenSchermTekst, new Vector2(FlashBackTextPosition.X + 2, FlashBackTextPosition.Y + 2), Color.Yellow);
                    spriteBatch.DrawString(FlashBacks, tussenSchermTekst, FlashBackTextPosition, Color.Black);
                    break;
                case Gamestate.EndCredits:
                    endCredits.Draw(spriteBatch);
                    break;
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
